package model.core;

import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JPanel;

import model.ability.HandlerFactory;
import model.area_effect.AreaEffectRemover;
import model.entity.Avatar;
import model.map.AvatarStateObserver;
import model.map.GameMap;
import model.map.GameStateObserver;
import model.map.MapPoint;
import model.map.ViewMap;

import view.ControllerConfigMenu;
import view.EquipmentPanel;
import view.GUITest;
import view.GamePanel;
import view.InventoryPanel;
import view.MainFrame;
import view.MainMenu;
import view.MapPanel;
import view.OccupationChooserMenu;
import view.PauseMenu;
import view.PausePanel;
import view.SkillsPanel;
import view.StatsPanel;
import view.Terminal;
import view.listeners.AcceptListener;
import view.listeners.BackListener;
import view.listeners.BuyListener;
import view.listeners.CancelListener;
import view.listeners.ContinueListener;
import view.listeners.ExitListener;
import view.listeners.GameReadyListener;
import view.listeners.NewGameListener;
import view.listeners.PauseExitListener;
import view.listeners.PauseListener;
import view.listeners.ResetListener;
import view.listeners.SellListener;
import view.listeners.SmasherListener;
import view.listeners.SneakListener;
import view.listeners.SummonerListener;
import view.listeners.ToControllerConfigMenuListener;
import view.listeners.ToOccupationChooserListener;


public class TestDriver {	
	public static void runGame(){
		
		FileParser parser = new FileParser("Data\\Instantiation.dat");

		GameInstantiation inst = new GameInstantiation();
		
		try {
			parser.parseAndInstantiate(inst);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		
		MainFrame mainFrame = new MainFrame("Medusa");
		
		InventoryPanel inventoryPanel = new InventoryPanel();
		MapPanel mapPanel = new MapPanel();
		PausePanel pausePanel = new PausePanel();
		EquipmentPanel equipPanel = new EquipmentPanel();
		SkillsPanel skillsPanel = new SkillsPanel();
		StatsPanel statsPanel = new StatsPanel();
		Terminal terminal = new Terminal();
		NewGameInstantiator newGameInstantiator = new NewGameInstantiator();
		LoadGameInstantiator loadGameInstantiator = new LoadGameInstantiator();
		Instantiator instantiator;
		Boolean newGame = new Boolean(false);
		Boolean gameReady = new Boolean(false);

		JPanel[] panels = new JPanel[] { inventoryPanel, mapPanel, pausePanel, equipPanel, skillsPanel, statsPanel };
		
		GamePanel gp = new GamePanel( panels );
		MainMenu menu = new MainMenu( mainFrame );
		ControllerConfigMenu configMenu = new ControllerConfigMenu( mainFrame );
		OccupationChooserMenu occupationMenu = new OccupationChooserMenu( mainFrame );
		PauseMenu pauseMenu = new PauseMenu();
		pauseMenu.setVisible(false);
		//LevelUpMenu levelUpMenu = new LevelUpMenu();
		//levelUpMenu.setVisible(false);
		//MerchantInteractionMenu merchantMenu = new MerchantInteractionMenu(null,null);
		gp.add(terminal);
		
		NewGameListener newGameListener = new NewGameListener( mainFrame );
		ExitListener exitListener = new ExitListener();
		ToControllerConfigMenuListener toControllerConfigListener= new ToControllerConfigMenuListener( mainFrame );
		ToOccupationChooserListener toOccupationChooserListener = new ToOccupationChooserListener( mainFrame, newGame );
		AcceptListener acceptListener = new AcceptListener( mainFrame );
		ResetListener resetListener = new ResetListener();
		CancelListener cancelListener = new CancelListener( mainFrame );
		BackListener backListener = new BackListener( mainFrame );
		PauseListener pauseListener = new PauseListener( gp, pauseMenu );
		//SaveListener saveListener = new SaveListener();
		//LoadListener loadListener = new LoadListener();
		ContinueListener continueListener = new ContinueListener( pauseMenu );
		PauseExitListener pauseExitListener = new PauseExitListener( mainFrame, pauseMenu );
		BuyListener buyListener = new BuyListener();
		SellListener sellListener = new SellListener();
		GameReadyListener gameReadyListener = new GameReadyListener(gameReady);
		
		SneakListener sneakListener = new SneakListener(newGameInstantiator);
		SummonerListener summonerListener = new SummonerListener(newGameInstantiator);
		SmasherListener smasherListener = new SmasherListener(newGameInstantiator);
		
		menu.addNewGameListener(toOccupationChooserListener);
		menu.addChangeControllerListener(toControllerConfigListener);
		menu.addExitListener(exitListener);
		//menu.addLoadListener(loadListener);
		
		occupationMenu.addNewGameListener(newGameListener);
		occupationMenu.addNewGameListener(gameReadyListener);
		occupationMenu.addBackListener(backListener);
		
		occupationMenu.addSneakListener(sneakListener);
		occupationMenu.addSummonerListener(summonerListener);
		occupationMenu.addSmasherListener(smasherListener);
		
		configMenu.addAcceptListener( acceptListener );
		configMenu.addResetListener( resetListener );
		configMenu.addCancelListener( cancelListener );
		
		pausePanel.addPauseListener( pauseListener );
		
		//pauseMenu.addSaveListener( saveListener );
		pauseMenu.addContinueListener( continueListener );
		pauseMenu.addExitListener( pauseExitListener );
		
		mainFrame.add( gp, "GAME_PANEL");
		mainFrame.add( menu, "MAIN_MENU");
		mainFrame.add( configMenu, "CONTROLLER_CONFIG_MENU");
		mainFrame.add( occupationMenu, "OCCUPATION_CHOOSER_MENU");
		
		mainFrame.changeActivePanel("MAIN_MENU");
		
		mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		System.out.println("Terrain: ");
		while (!gameReady){
			System.out.println(newGame);
		}
		GameScheduler.getInstance().startTimer();
		if (newGame){
			instantiator = newGameInstantiator;
		}
		else{
			instantiator = loadGameInstantiator;
		}
		System.out.println("Terrain: ");
		GameMap map = GameSetter.getGameMap(instantiator);
		HandlerFactory handlerFactory = new HandlerFactory(map);
		AreaEffectRemover aeRemover = new AreaEffectRemover(map);
		System.out.println("Terrain: " + map.getTerrain(new MapPoint(0,2)).getGraphicID());
		ViewMap viewMap = GameSetter.getViewMap(instantiator, map);
		System.out.println("Terrain: " + map.getTerrain(new MapPoint(0,2)).getGraphicID());
		Avatar avatar = instantiator.getAvatar();
		System.out.println("Terrain: " + map.getTerrain(new MapPoint(0,2)).getGraphicID());
		AvatarStateObserver avatarStateObserver = new AvatarStateObserver(avatar, map);
		GameStateObserver gameStateObserver = new GameStateObserver(map, viewMap, avatar);

		System.out.println("Terrain: " + map.getTerrain(new MapPoint(0,2)).getGraphicID());
		mapPanel.setMap(viewMap);
		
		while(true){}
	}
}
